有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVAlang.RuntimeException:Android Studio

错误如下。 当我创建代码并试图使其工作时,我得到了以下错误。 Libraly的零碎交易中的R.id.容器。替换(R.id.container,fragment)似乎不可用。 我不知道为什么我不能获取xml<;片段id=容器。 尝试过,将AppCompatActivity更改为FragmentActivity/Fragment等,但没有修复

你能给我一些解决方案吗

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.animalbook/com.example.animalbook.Libraly}: 安卓.view.InflateException: Binary XML file line #58: Binary XML file line #58: Error inflating class fragment
        at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at 安卓.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at 安卓.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at 安卓.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at 安卓.os.Handler.dispatchMessage(Handler.java:106)
        at 安卓.os.Looper.loop(Looper.java:193)
        at 安卓.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: 安卓.view.InflateException: Binary XML file line #58: Binary XML file line #58: Error inflating class fragment
     Caused by: 安卓.view.InflateException: Binary XML file line #58: Error inflating class fragment
     Caused by: java.lang.NullPointerException

package com.example.animalbook

import 安卓x.appcompat.app.AppCompatActivity
import 安卓.os.Bundle
import 安卓.view.LayoutInflater
import 安卓.view.View
import 安卓.view.ViewGroup
import 安卓.widget.ImageView
import 安卓x.fragment.app.Fragment
import 安卓x.fragment.app.FragmentActivity
import kotlinx.安卓.synthetic.main.activity_libraly.*

class Libraly : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_libraly)

        lionButton.setOnClickListener {
            val fragment = LioneFragment()
            val fragmentManager = this.supportFragmentManager
            val fragmentTransaction = fragmentManager.beginTransaction()
            fragmentTransaction.replace(R.id.container, fragment).addToBackStack(null)
        }

        hippoButton.setOnClickListener {
            val fragment = HippoFragment()
            val fragmentManager = this.supportFragmentManager
            val fragmentTransaction = fragmentManager.beginTransaction()
            fragmentTransaction.replace(R.id.container, fragment).addToBackStack(null).commit()
        }

        giraffeButton.setOnClickListener {
            val fragment = GiraffeFragment()
            val fragmentManager = this.supportFragmentManager
            val fragmentTransaction = fragmentManager.beginTransaction()
            fragmentTransaction.replace(R.id.container,fragment).addToBackStack(null).commit()
        }

        val fragment = titlefragment as? TitleFragment
        fragment?.setTitle("図鑑")
    }
}
<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".Libraly">

    <fragment
        安卓:id="@+id/titlefragment"
        安卓:name="com.example.animalbook.TitleFragment"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout="@layout/fragment_title" />

    <LinearLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:gravity="center"
        安卓:layout_marginBottom="350dp"
        安卓:orientation="horizontal"
        安卓:id="@+id/Animalbutoon">

        <Button
            安卓:id="@+id/lionButton"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="@string/lione_text"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            安卓:id="@+id/hippoButton"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:layout_marginLeft="24dp"
            安卓:layout_marginRight="24dp"
            安卓:text="@string/hippo_text"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            安卓:id="@+id/giraffeButton"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="@string/giragge_text"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </LinearLayout>

    <fragment
        安卓:id="@+id/container"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:layout_marginTop="225dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</安卓x.constraintlayout.widget.ConstraintLayout>

共 (0) 个答案